package cn.wen.core.http;

import android.app.Activity;

import java.util.Map;

import cn.wen.base.view.banner.DefActivityLifecycleCallbacks;
import cn.wen.core.http.callback.OkHttpCallback;
import cn.wen.core.http.callback.OkHttpFileCallback;


/**
 * OkHttp网络请求的业务类
 */

public class OkHttpPresenter {
    private final HttpUtils mHttpsUtils;
    private Activity mContext;

    public OkHttpPresenter(Activity context) {
        mHttpsUtils = new HttpUtils(context);
        this.mContext = context;
        if (mContext != null) {
            try{
                mContext.getApplication().registerActivityLifecycleCallbacks(new DefActivityLifecycleCallbacks() {
                    @Override
                    public void onActivityDestroyed(Activity activity) {
                        if (mContext == activity) {
                            if (mHttpsUtils != null) {
                                mHttpsUtils.cancel();
                            }
                            mContext.getApplication().unregisterActivityLifecycleCallbacks(this);
                        }
                        mContext = null;
                    }
                });
            }catch (Exception e){

            }

        }
    }

    /**
     * get请求
     *
     * @param url      请求的URL
     * @param params   请求的参数
     * @param callback 回调
     * @param isCache  是否要缓存
     * @param <T>
     */
    public <T> void get(String url, Map<String, Object> params, OkHttpCallback<T> callback, boolean isCache) {
        mHttpsUtils
                .url(url)
                .type(HttpUtils.RequestType.GET)
                .params(params)
                .cache(isCache)
                .execute(callback);
    }

    /**
     * post请求
     *
     * @param url      请求的URL
     * @param data     请求的参数
     * @param callback 回调
     * @param isCache  是否要缓存
     * @param <T>
     */
    public <T> void post(String url, Object data, OkHttpCallback<T> callback, boolean isCache) {
        mHttpsUtils.url(url)
                .params("data", data)
                .cache(isCache)
                .execute(callback);
    }

    /**
     * Post请求
     */
    public <T> void post(String url, Object data, OkHttpCallback<T> callback) {
        post(url, data, callback, true);
    }

    /**
     * 文件上传
     *
     * @param url      上传的URL
     * @param params   上传的参数
     * @param callback 回调
     */
    public <T> void upload(String url, Map<String, Object> params, OkHttpCallback<T> callback) {
        mHttpsUtils.url(url)
                .type(HttpUtils.RequestType.UPLOAD)
                .params(params)
                .execute(callback);
    }

    /**
     * 文件下载
     *
     * @param url      下载URL
     * @param fileDir  文件目录名称
     * @param fileName 文件名称
     * @param callback 回调
     */
    public void download(String url, String fileDir, String fileName, OkHttpFileCallback callback) {
        mHttpsUtils.url(url)
                .type(HttpUtils.RequestType.DOWNLOAD)
                .fileDir(fileDir)
                .fileName(fileName)
                .execute(callback);
    }

    public void cancel() {
        mHttpsUtils.cancel();
    }
}
